<?php
namespace app\agent\controller;

use app\common\cache\credit\ICreditCache;
use app\common\cache\user\IUserCache;
use app\common\controller\AgentBaseController;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;
use think\Db;

class CreditFavorite extends AgentBaseController {

    /**
     * @功能：信用卡收藏
     * @开发者：cxl
     * @return string
     */
    public function credit_favorite_list(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $credit_product_id = IRequest::get("credit_product_id");

        //取得数据
        $credit_favorite_where['cf.is_delete'] = IsDelete::No;
        $credit_favorite_where['ct.agent_admin_id'] = get_login_agent_admin_id();
        if(!empty($user_id)) $credit_favorite_where['cf.user_id'] = $user_id;
        if(!empty($credit_product_id)) $credit_favorite_where['cf.credit_product_id'] = $credit_product_id;
        $credit_favorite_list = IDb::getInstance("credit_favorite as cf")
            ->setDbFiled("cf.*")
            ->setDbJoin("credit_product as ct","cf.credit_product_id=ct.credit_product_id")
            ->setDbWhere($credit_favorite_where)
            ->setDbOrder('cf.credit_favorite_id desc')
            ->pag();

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_favorite_list",$credit_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡收藏
     * @开发者：cxl
     * @return string
     */
    public function credit_favorite_del_list(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $credit_product_id = IRequest::get("credit_product_id");

        //取得数据
        $credit_favorite_where['cf.is_delete'] = IsDelete::Yes;
        $credit_favorite_where['ct.agent_admin_id'] = get_login_agent_admin_id();
        if(!empty($user_id)) $credit_favorite_where['cf.user_id'] = $user_id;
        if(!empty($credit_product_id)) $credit_favorite_where['cf.credit_product_id'] = $credit_product_id;
        $credit_favorite_list = IDb::getInstance("credit_favorite as cf")
            ->setDbFiled("cf.*")
            ->setDbJoin("credit_product as ct","cf.credit_product_id=ct.credit_product_id")
            ->setDbWhere($credit_favorite_where)
            ->setDbOrder('cf.credit_favorite_id desc')
            ->pag();


        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("credit_favorite_list",$credit_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡收藏详情
     * @开发者：cxl
     * @return string
     */
    public function credit_favorite_info(){
        //接收参数
        $credit_favorite_id = IRequest::get("credit_favorite_id",IRequest::NOT_EMPTY,"信用卡收藏编码不能为空！");

        //信用卡收藏详情
        $credit_favorite_where['credit_favorite_id'] = $credit_favorite_id;
        $credit_favorite_info = IDb::getInstance("credit_favorite")->setDbWhere($credit_favorite_where)->row();
        if($credit_favorite_info === false){
            error("取得信用卡收藏信息失败！");
        }

        //信用卡收藏详情
        $user_info = IUserCache::getInfo($credit_favorite_info['user_id']);
        if($user_info === false){
            error("取得信用卡收藏信息失败！");
        }

        //信用卡收藏详情
        $credit_product_info = ICreditCache::getInfo($credit_favorite_info['credit_product_id']);
        if($credit_product_info === false){
            error("取得信用卡收藏信息失败！");
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("credit_favorite_info",$credit_favorite_info);
        $this->assign("user_info",$user_info);
        $this->assign("credit_product_info",$credit_product_info);

        //渲染模板输出
        return view();
    }
}
